'use strict';

const Controller = require('egg').Controller;

class HomeController extends Controller {
  async index() {
    const { ctx } = this;
    // const { id } = ctx.query;
    // ctx.body = 'hi, egg ' + id;
    await ctx.render('index.html', {
      title: '敲出真谛',
    });
  }
  async carousel() {
    const { ctx } = this;
    const result = await ctx.service.home.carousel();
    ctx.body = result;
  }
  async add() {
    const { ctx } = this;
    const { title } = ctx.request.body;
    ctx.body = {
      title,
    };
  }
  async addCarousel() {
    const { ctx } = this;
    const { carousel_id, imgPath, describes, id, create_date, modify_date, img_path } = ctx.request.body;
    try {
      await ctx.service.home.addCarousel(carousel_id, imgPath, describes, id, create_date, modify_date, img_path);
      ctx.body = {
        status: 80200,
        message: '添加成功',
        data: null,
      };
    } catch (error) {
      ctx.body = {
        status: 500,
        message: '添加失败',
        data: null,
      };
    }
  }
  async editCarousel() {
    const { ctx } = this;
    const { carousel_id, describes } = ctx.request.body;
    try {
      await ctx.service.home.editCarousel(carousel_id, describes);
      ctx.body = {
        status: 80200,
        message: '修改成功',
        data: null,
      };
    } catch (error) {
      ctx.body = {
        status: 80200,
        message: '修改失败',
        data: null,
      };
    }
  }
  async deleteCarousel() {
    const { ctx } = this;
    const { id } = ctx.request.body;
    try {
      await ctx.service.home.deleteCarousel(id);
      ctx.body = {
        status: 80200,
        message: '删除成功',
        data: null,
      };
    } catch (error) {
      ctx.body = {
        status: 80200,
        message: '删除失败',
        data: null,
      };
    }
  }
}

module.exports = HomeController;
